package matcherNotes;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class matcherDemo {

    private String state;
    private String province;
    private String city;
    private String county;
    private String town;
    private String village;

    public void toShow() {
        System.out.println(state);
        System.out.println(province);
        System.out.println(city);
        if(county != null)
            System.out.println(county);
        if(town != null)
            System.out.println(town);
        if(village != null)
            System.out.println(village);
    }

    /**
     * 在给定的地址中使用正则表达式提取国家、省份、城市、区、街道、门牌号，
     * @param address
     */
    public void addressResolution(String address) {
        String regex = "(?<state>.*国)((?<province>[^省]+省|.+自治区)|上海|北京|天津|重庆)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|.+区)(?<town>.*镇|.*街)(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        while (m.find()) {
            state = m.group("state");
            province = m.group("province");
            city = m.group("city");
            county = m.group("county");
            town = m.group("town");
            village = m.group("village");
        }
        toShow();
    }

}
